package com.web.order.util;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;


import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ExportExcel {
	private OutputStream os;

	private int colWidth = 30;

	private int rowView = 300;

	/**
	 * 写入数据
	 * 
	 * @param resultList
	 * @return
	 * @throws IOException
	 * @throws RowsExceededException
	 * @throws WriteException
	 * 
	 */
	@SuppressWarnings("rawtypes")
	private WritableWorkbook export(List resultList) throws IOException,
			RowsExceededException, WriteException {

		WritableWorkbook wwb = Workbook.createWorkbook(os);
		WritableSheet ws = wwb.createSheet("Sheet1", 0);
		
		// 添加表头
		ws.addCell(new Label(0, 0, "订单编号", getTitle()));
		ws.setColumnView(0, this.colWidth);

		ws.addCell(new Label(1, 0, "下单用户", getTitle()));
		ws.setColumnView(1, this.colWidth);

		ws.addCell(new Label(2, 0, "下单宾馆", getTitle()));
		ws.setColumnView(2, this.colWidth);

		ws.addCell(new Label(3, 0, "开始日期", getTitle()));
		ws.setColumnView(3, this.colWidth);

		ws.addCell(new Label(4, 0, "结束日期", getTitle()));
		ws.setColumnView(4, this.colWidth);

		ws.addCell(new Label(5, 0, "消费说明", getTitle()));
		ws.setColumnView(5, this.colWidth);

		ws.addCell(new Label(6, 0, "消费金额", getTitle()));
		ws.setColumnView(6, this.colWidth);

		ws.addCell(new Label(7, 0, "下单日期", getTitle()));
		ws.setColumnView(7, this.colWidth);
		
		ws.addCell(new Label(8, 0, "下单状态", getTitle()));
		ws.setColumnView(8, this.colWidth);
		
		ws.addCell(new Label(9, 0, "下单评论", getTitle()));
		ws.setColumnView(9, this.colWidth);
		
		ws.setRowView(1, this.rowView);

		// 添加数据
		if (resultList != null && resultList.size() > 0) {
			for (int row = 0; row < resultList.size(); row++) {
				List data = (List) resultList.get(row);
				for (int col = 0; col < 10; col++) { //注意这个长度
					Object obj = data.get(col);
					Label l = new Label(col, row + 1, obj == null ? ""
							: obj.toString(), getNormolCell());
					ws.addCell(l);
				}
				ws.setRowView(row + 1, this.rowView);
			}
		}

		return wwb;
	}

	/** */
	/**
	 * 设置标题样式
	 * 
	 * @return
	 */
	public WritableCellFormat getTitle() {
		WritableFont font = new WritableFont(WritableFont.TIMES, 14);
		try {
			font.setColour(Colour.BLUE);// 蓝色字体
		} catch (WriteException e1) {
			// TODO 自动生成 catch 块
			e1.printStackTrace();
		}
		WritableCellFormat format = new WritableCellFormat(font);

		try {
			format.setAlignment(jxl.format.Alignment.CENTRE);
			format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
		} catch (WriteException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return format;
	}

	/** */
	/**
	 * 设置其他单元格样式
	 * 
	 * @return
	 */
	public WritableCellFormat getNormolCell() {// 12号字体,上下左右居中,带黑色边框
		WritableFont font = new WritableFont(WritableFont.TIMES, 12);
		WritableCellFormat format = new WritableCellFormat(font);
		try {
			format.setAlignment(jxl.format.Alignment.CENTRE);
			format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
		} catch (WriteException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return format;
	}

	@SuppressWarnings("rawtypes")
	public void writeExcel(final OutputStream os, final List resultList) {
		this.os = os;
		try {
			WritableWorkbook wwb = this.export(resultList);
			wwb.write();
			wwb.close();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void close() throws IOException {
		if (this.os != null) {
			this.os.close();
		}
	}

	public int getColWidth() {
		return colWidth;
	}

	public void setColWidth(int colWidth) {
		this.colWidth = colWidth;
	}

	public int getRowView() {
		return rowView;
	}

	public void setRowView(int rowView) {
		this.rowView = rowView;
	}
}
